import { auditManageConst } from "src/consts/audit_manage_const";
import * as Api from 'src/api/net';
import { PopupName } from "src/consts/popup_const";

const state: any = {} as any;
// StateIniter.init(state, 'demo');

state.curr_page = 1;
state.page_size = 15;
state.total_page = 1;
state.total_size = 1;
state.data_list = []
state.selectList = [];
// vuex

/** 更新数据的 */
const mutations = {
    [auditManageConst.CommitAuditMList](state: any, data: any): void {
        console.log("auditmanage data update", data);
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: DemoData = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;
    },

    [auditManageConst.CommitType](state: any, data: any[]): void {
        state.selectList = data
        WLLib.panel(PopupName.AuditM_Audit);
    }
}

//异步处理
const actions = {
    [auditManageConst.DispatchAuditMList](context: VuexData, data?: any): void {
        var authority = getUserInfo().authority_list
        var req = ""
        var flag = false
        for (let i = 0; i < authority.length; i++) {
            if (authority[i].name == "公共网站管理") {
                for (let j = 0; j < authority[i].child_list.length; j++) {
                    if (authority[i].child_list[j].name == "审核管理") {
                        for (let k = 0; k < authority[i].child_list[j].child_list.length; k++) {
                            if (authority[i].child_list[j].child_list[k].id == "ROLE_webaudit_audit_admin_list") {
                                flag = true
                            }
                        }
                    }
                }
            }
        }

        if (flag) {
            req = "/webaudit/audit/admin/list.do"
        } else {
            req = "/webaudit/audit/normal/list.do"
        }

        Api.AjaxPost(Config.ajaxUrl + req, data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(auditManageConst.CommitAuditMList, res.data);
            }
        })
    },
    [auditManageConst.DispatchAuditMAudit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/webaudit/audit/do.do", data, (res: any) => {
            if (data.isLast) {
                let sendData: any = {}
                sendData["curr_page"] = state.curr_page
                sendData["page_size"] = state.page_size
                WLDispatch(auditManageConst.DispatchAuditMList, sendData);
                WLMessage("审核成功", "success")
                WLLib.panel(PopupName.AuditM_Audit).close();
                WLLib.panel(PopupName.AuditM_Status).close();
            }
        })
    }
}


export default {
    state,
    mutations,
    actions
}